﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace NaturalNote.Core
{
    public class FastInvokerHelper
    {
        private readonly static ConcurrentDictionary<string, MemberProperty> memberPropertyDic;
        static FastInvokerHelper()
        {
            memberPropertyDic = new ConcurrentDictionary<string, MemberProperty>();
        }
        public static MemberProperty TryGetMemberProperty(string key, Func<MemberProperty> getMemberProperty)
        {
            MemberProperty mp = null;
            if (memberPropertyDic.TryGetValue(key, out mp))
            {
                return mp;
            }
            mp = getMemberProperty();
            memberPropertyDic.TryAdd(key, mp);
            return mp;
        }
        public static MemberProperty GetMemberProperty(Type t, string name)
        {
            string key = $"{t.FullName}_{name}";
            return TryGetMemberProperty(key, ()=> {
                var pi = t.GetProperty(name);
                if (pi == null)
                {
                    return null;
                }
                return new MemberProperty(pi);
            });
        }
    }
}
